package org.jeecg.talentCooperation.fruit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.talentCooperation.common.vo.Result;
import org.jeecg.talentCooperation.fruit.entity.Fruit;
import org.jeecg.talentCooperation.fruit.entity.FruitAuthor;
import org.jeecg.talentCooperation.fruit.entity.FruitAuthorOrg;
import org.jeecg.talentCooperation.fruit.model.FruitCommonResultModel;
import org.jeecg.talentCooperation.fruit.model.FruitInfoResultModel;
import org.jeecg.talentCooperation.fruit.model.FruitResultModel;
import org.jeecg.talentCooperation.fruit.model.ResearchFieldRelatedResultModel;
import org.jeecg.talentCooperation.fruit.service.IFruitAuthorOrgService;
import org.jeecg.talentCooperation.fruit.service.IFruitAuthorService;
import org.jeecg.talentCooperation.fruit.service.IFruitResultService;
import org.jeecg.talentCooperation.fruit.service.IFruitService;
import org.jeecg.talentCooperation.talent.entity.TalentBasic;
import org.jeecg.talentCooperation.talent.model.TalentFruitResultModel;
import org.jeecg.talentCooperation.talent.model.TalentResultModel;
import org.jeecg.talentCooperation.util.SnowflakeIdUtil;
import org.jeecg.talentCooperation.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 人才协作
 * @Author: jeecg-boot
 * @Date:   2023-8-17
 * @Version: V1.0
 */
@Api(tags="人才协作")
@RestController
@RequestMapping("/ndTalent/fruit/manage")
@Slf4j
public class FruitManageController {
	@Autowired
	private IFruitService fruitService;
	@Autowired
	private IFruitAuthorService fruitAuthorService;
	@Autowired
	private IFruitAuthorOrgService fruitAuthorOrgService;

	@PostMapping(value = "/list")
	public Result<?> list(@RequestBody FruitInfoResultModel fruit) {


		Integer pageNo = fruit.getPageNo();
		Integer pageSize = fruit.getPageSize();

		fruit.setPageNo((pageNo - 1) * pageSize);
		fruit.setPageSize(pageSize);

		Page<Fruit> talentFruitPageList = new Page<Fruit>(pageNo, pageSize);
		talentFruitPageList = fruitService.getFruitList(talentFruitPageList, fruit);

		return Result.OK(talentFruitPageList);
	}


	@PostMapping(value = "/detail")
	public Result<?> detail(@RequestBody FruitInfoResultModel fruit) {

		FruitInfoResultModel detailFruit = fruitService.getDetailFruitById(fruit.getId());

		return Result.OK(detailFruit);
	}

	@Transactional(rollbackFor = Exception.class)
	@PostMapping(value = "/saveOrUpdate")
	public Result<?> saveOrUpdate(@RequestBody FruitInfoResultModel fruit) {
		if(fruit.getId() == null){
			SnowflakeIdUtil idWorker = new SnowflakeIdUtil(1, 1);
			fruit.setId(idWorker.nextId());
		}
		Fruit fruitOld = fruitService.getById(fruit.getId());

		//查询原来的作者
		QueryWrapper<FruitAuthor> wrapper = new QueryWrapper<>();
		wrapper.eq("fruit_id", fruit.getId());
		List<FruitAuthor> oldAuthorList = fruitAuthorService.list(wrapper);
		oldAuthorList.forEach(fruitAuthor -> {
			QueryWrapper<FruitAuthorOrg> orgWrapper = new QueryWrapper<>();
			orgWrapper.eq("author_id", fruitAuthor.getId());
			//删除原来的作者关联单位
			fruitAuthorOrgService.remove(orgWrapper);
		});
		//删除原来的作者
		fruitAuthorService.remove(wrapper);

		//新增作者及作者单位
		List<FruitAuthor> newAuthorList = fruit.getAuthorList();
		newAuthorList.forEach(fruitAuthor -> {
			List<FruitAuthorOrg> newAuthorOrgList = fruitAuthor.getOrgPOList();
			fruitAuthorOrgService.saveOrUpdateBatch(newAuthorOrgList);
		});
		fruitAuthorService.saveOrUpdateBatch(newAuthorList);


		fruitOld.setName(fruit.getName());
		fruitOld.setType(fruit.getType());
		fruitOld.setPaperType(fruit.getPaperType());
		fruitOld.setPubPlace(fruit.getPubPlace());
		fruitOld.setPubTime(fruit.getPubTime());
		fruitOld.setSummary(fruit.getSummary());

		fruitService.saveOrUpdate(fruitOld);
		return Result.OK();
	}


	@PostMapping(value = "/delete")
	public Result<?> delete(@RequestBody FruitInfoResultModel fruit) {
		Fruit fruitOld = fruitService.getById(fruit.getId());
		fruitOld.setIsDel(true);
		fruitService.updateById(fruitOld);
		return Result.OK();
	}



}
